package com.xintool.mapper;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class PetLoader {

    private static final Map<Integer,List<Integer>> pets=new HashMap<>();

    // 初始化块：程序启动时自动加载
    static {
        loadPetsFromFile("data/pets500.txt");
    }

    private static void loadPetsFromFile(String filePath) {
        // 直接使用文件系统路径
        File file = new File(filePath); // 根目录下的data文件夹

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                processLine(line);
            }
        } catch (IOException e) {
            System.err.println("加载失败，请确认文件存在且路径正确：" + file.getAbsolutePath());
        }
    }

    private static void processLine(String line) {
        // 跳过空行和注释行
        if (line.trim().isEmpty() || line.startsWith("#")) return;

        String[] parts = line.split(",");
        if (parts.length < 8) {
            System.err.println("无效数据行: " + line);
            return;
        }

        try {
            Integer id = Integer.valueOf(parts[0].trim());
            //String name = parts[1].trim();
            List<Integer> stats = Arrays.stream(parts, 2, 8)
                    .map(String::trim)
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());

            pets.put(id, stats);
        } catch (NumberFormatException e) {
            System.err.println("数值格式错误: " + line);
        }
    }
    public Map<Integer,List<Integer>> getPets(){
        return pets;
    }
}
